import Vue from 'vue';
import Toast from '@/components/widget/Toast';

let cmp = undefined;

const init = () => {
    close();
    cmp = new Vue(Toast);
    document.body.appendChild(cmp.$mount().$el);
    return cmp;
};

const close = () => {
    if (cmp !== undefined) {
        try {
            cmp.close();
        } catch (e) {
            console.info(e);
        }
    }
    cmp = undefined;
};

export const toast = {
    error(message) {
        init().show(message, 'error')
    },
    success(message) {
        init().show(message, 'success')
    },
    info(message) {
        init().show(message, 'info')
    },
    primary(message) {
        init().show(message, 'primary')
    },
    warning(msg) {
        init().show(msg, 'warning')
    },
    secondary(msg) {
        init().show(msg, 'secondary')
    }
};

Vue.prototype.$toast = toast;

export default toast;